//
//  BQSearchResultController.m
//  BeeQuick
//
//  Created by Asun on 2017/7/11.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQSearchResultController.h"
#import "BQAccountManager.h"
#import "BQProductModel.h"
#import "UIColor+MLAddition.h"
#import "BQShopCollectionViewCell.h"
#import  "BQShopTableViewController.h"
#import  "BQShopModelArrayManager.h"
@interface BQSearchResultController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong)NSArray<BQProductModel *>* shopModel;

@property (weak, nonatomic)UIButton   *btn;

@property (weak, nonatomic) UICollectionView *collectionView;

@end

@implementation BQSearchResultController

- (void)viewDidLoad {
    
    
    [super viewDidLoad];
    
    [self setupCollection];
    
    [self loadData];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    
}
#pragma mark - 加载数据 -
- (void)loadData{
    
//    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
//    
//    [dict setValue:@"8" forKey:@"call"];
//    
//    [[BQAccountManager sharedInstance] POSTWithUrlString:searchResult_url
//                                              parameters:dict success:^(id responseObject) {
//                                                  self.shopModel = [NSArray yy_modelArrayWithClass:[BQProductModel class] json:responseObject[@"data"]];
//                                                  
//                                                  dispatch_async(dispatch_get_main_queue(), ^{
//                                                      [self setupCollection];
//                                                     
//                                                  });
//       
//                                              } failure:^(NSError *error) {
//                                                  
//                                                  NSLog(@"%@",error);
//                                                  
//                                              }];
    
    [[BQAccountManager sharedInstance] loadSearchProductResultsWithSuccessBlock:^{
                
        [self.collectionView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
        NSLog(@"%@", error);
        
    }];
}

- (void)setupCollection{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 1;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - 5) / 2;
    
    flowLayout.itemSize = CGSizeMake(width, 245);
    
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    
    _collectionView = collectionView;
    
    collectionView.backgroundColor = [UIColor whiteColor];
    
    [collectionView registerNib:[UINib nibWithNibName:@"BQShopCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Identifier"];
    
    collectionView.delegate = self;
    
    collectionView.dataSource = self;
    
    [self.view addSubview:collectionView];
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
    }];
    
    UIButton *btn = [[UIButton alloc]init];
    
    _btn = btn;
    
    [btn setImage:[UIImage imageNamed:@"v2_shop_empty"] forState:UIControlStateNormal];
    
    [self.view addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.offset(-30);
        make.bottom.offset(-30);
        
        make.height.width.offset(50);
    }];
    
    [self registerNoti];
    
    
    [btn addTarget:self action:@selector(presentController) forControlEvents:UIControlEventTouchUpInside];

    
    
}

- (void)registerNoti{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionBtn:) name:ToClickButtonNotificationName object:nil];

}

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)presentController{
    
    BQShopTableViewController *shop = [[BQShopTableViewController alloc]init];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:shop];
    
    [self.navigationController presentViewController:nav animated:YES completion:nil];
    
}

- (void)actionBtn:(NSNotification *)noti{
    
    UIButton *addBtn = noti.object;
    
    [addBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)clickBtn:(UIButton *)btn{
    
    [UIView animateWithDuration:0.2 animations:^{
        self.btn.transform = CGAffineTransformScale(self.btn.transform, 1.2, 1.2);
    } completion:^(BOOL finished) {
        self.btn.transform = CGAffineTransformIdentity;
    }];
    
}

#pragma mark - (每个CollectionView 有多少组)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}
#pragma mark
#pragma mark - (每一组有多少行)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    return [BQAccountManager sharedInstance].searchResults.count;
}

#pragma mark
#pragma mark - (每一行cell 的显示)
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    BQShopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Identifier" forIndexPath:indexPath];
    
    
    cell.model = [BQAccountManager sharedInstance].searchResults[indexPath.row];
    
    //cell添加圆角阴影
    cell.contentView.layer.cornerRadius = 2.0f;
    cell.contentView.layer.borderWidth = 1.0f;
    cell.contentView.layer.borderColor = [UIColor clearColor].CGColor;
    cell.contentView.layer.masksToBounds = YES;
    
    cell.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    cell.layer.shadowOffset = CGSizeMake(0, 2.0f);
    cell.layer.shadowRadius = 2.0f;
    cell.layer.shadowOpacity = 1.0f;
    cell.layer.masksToBounds = NO;
    cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath;

    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{

    cell.transform  = CGAffineTransformMakeTranslation(0, 80);
    
    [UIView animateWithDuration:0.5 animations:^{
        cell.transform = CGAffineTransformIdentity;
    }];

}


@end
